package com.liu.activemq.spring;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

@Service
public class SpringMQ_Produce {

    @Autowired
    //操作MQ的工具
    private JmsTemplate template;

    public static void main(String[] args) {
        //读取配置文件
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean
        SpringMQ_Produce produce = ctx.getBean(SpringMQ_Produce.class);

        //发送信息
        produce.template.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                TextMessage text = session.createTextMessage("Spring和Active MQ整合并发送消息");
                return text;
            }
        });
        System.out.println("发送成功");
    }
}
